package com.you.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.you.common.QueryPageParam;
import com.you.common.Result;
import com.you.entity.Goodstype;
import com.you.entity.Storage;
import com.you.service.impl.StorageServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 友友
 * @since 2022-10-24
 */
@RestController
@RequestMapping("/storage")
public class StorageController {

    @Autowired
    StorageServiceImpl storageService;

    @GetMapping("/list")
    public Result list(){
        List<Storage> list = storageService.list();
        return Result.suc(list);
    }

    //新增
    @PostMapping("/save")
    public Result save(@RequestBody Storage storage) {
        return storageService.save(storage)? Result.suc():Result.fail();
    }

    //修改
    @PostMapping("/mod")
    public Result update(@RequestBody Storage storage) {
        return storageService.updateById(storage)? Result.suc():Result.fail();
    }

    //删除
    @GetMapping("/del")
    public Result del(@RequestParam Integer id) {
        return storageService.removeById(id)? Result.suc():Result.fail();
    }

    //自定义分页(查询)
    @PostMapping("/diyListPage")
    public Result diyListPage(@RequestBody QueryPageParam pageParam) {

        HashMap param = pageParam.getParam();
        String name = (String) param.get("name");

        Page<Storage> page = new Page<>();
        page.setCurrent(pageParam.getPageNum());
        page.setSize(pageParam.getPageSize());

        LambdaQueryWrapper<Storage> wrapper = new LambdaQueryWrapper<>();

        if (StringUtils.isNotBlank(name)){
            wrapper.like(Storage::getName, name);
        }

        IPage result = storageService.pageDiy(page, wrapper);

        return Result.suc(result.getRecords(),result.getTotal());

    }
}

